package huoye.shops.huoye.operation.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.PullToRefreshActivity;
import huoye.shops.huoye.operation.adapter.ManageShopPrizeAdapter;
import huoye.shops.huoye.operation.model.ShopPrizeList;
import huoye.shops.huoye.operation.presenter.ManagePrizePresenter;
import huoye.shops.huoye.operation.view.IManagePrizeRefreshView;

public class ManagePrizeActivity extends PullToRefreshActivity<ShopPrizeList> implements IManagePrizeRefreshView {

    public static final String EXTRAS_KEY_PRIZE_TYPE = "EXTRAS_KEY_PRIZE_TYPE";
    public static final String EXTRAS_KEY_PRIZE_SELECT = "1";
    public static final String EXTRAS_KEY_PRIZE_MANAGE = "2";


    public static final String EXTRAS_KEY_PRIZE_SELECT_STOCK = "EXTRAS_KEY_PRIZE_SELECT_STOCK";
    public static final String EXTRAS_KEY_PRIZE_SELECT_STOCK_NUM = "EXTRAS_KEY_PRIZE_SELECT_STOCK_NUM";
    public static final String EXTRAS_KEY_PRIZE_SELECT_IMAGE= "EXTRAS_KEY_PRIZE_SELECT_IMAGE";
    public static final String EXTRAS_KEY_PRIZE_SELECT_PRIZE_ID = "EXTRAS_KEY_PRIZE_SELECT_PRIZE_ID";

    public static final int EXTRAS_INTENT_PRIZE_TYPE_URL = 1;


    @BindView(R.id.operation_manage_et_seek)
    EditText etSeek;
    @BindView(R.id.operation_manage_prize_btn_add_shop)
    Button btnAddShop;


    private String type;

    @OnClick(R.id.operation_manage_prize_btn_add_shop)
    void onAddShopClick(){
        startActivity(EditAddShopActivity.class);
    }



    private ManagePrizePresenter mManagePrizePresenter;
    private ManageShopPrizeAdapter adapter;

    @Override
    public RecyclerViewAdapter createAdapter() {
        setLayoutManager();
        adapter = new ManageShopPrizeAdapter(this,type);
        return adapter;
    }

    @Override
    public void onItemClick(ShopPrizeList shopPrizeList, int i, View view) {
        if(type.equals(EXTRAS_KEY_PRIZE_SELECT)){
            Intent selectIntent = new Intent();
            selectIntent.putExtra(EXTRAS_KEY_PRIZE_SELECT_STOCK,shopPrizeList.getPrizeNum());
            selectIntent.putExtra(EXTRAS_KEY_PRIZE_SELECT_IMAGE,shopPrizeList.getPrizeImg());
            selectIntent.putExtra(EXTRAS_KEY_PRIZE_SELECT_PRIZE_ID,shopPrizeList.getPrizeId());
            setResult(EXTRAS_INTENT_PRIZE_TYPE_URL,selectIntent);
            finish();
        }
    }

    @Override
    protected int getContentViewId() {
       return R.layout.activity_manage_prize;
    }

    private String PrizeType;

    @Override
    protected void initView(@Nullable Bundle bundle) {
        super.initView(bundle);
        type = getIntent().getStringExtra(EXTRAS_KEY_PRIZE_TYPE);
        if(type.equals(EXTRAS_KEY_PRIZE_SELECT)){
            setTitle("选择奖品");
            btnAddShop.setVisibility(View.GONE);
            PrizeType = "active";
        }else {
            setTitle("奖品管理");
            PrizeType = "";
        }


        mManagePrizePresenter = new ManagePrizePresenter();
        mManagePrizePresenter.addTaskListener(this);
        mManagePrizePresenter.onStart();
        query();

        etSeek.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {

                /*判断是否是“GO”键*/
                if((actionId == 0 || actionId == 3) && keyEvent != null){
                    /*隐藏软键盘*/
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(
                                v.getApplicationWindowToken(), 0);
                    }

            //        makeToast("你点击了确定键");
                    String seek = etSeek.getText().toString();
                    mManagePrizePresenter.getPrizeList(seek,new Paging(),PrizeType);


                    return true;
                }

                return false;
            }
        });

    }


    @Override
    public void onResume() {
        super.onResume();
        if(mManagePrizePresenter != null ){
            query();
        }
    }

    @Override
    public void query(Paging paging) {
        super.query(paging);
        //奖品管理
        mManagePrizePresenter.getPrizeList("",paging,PrizeType);
    }


}
